﻿#include "SerialPortWidget.h"

#include <QDebug>
#include <QDateTime>

namespace Core {

	SerialPortWidget::SerialPortWidget(QWidget *parent)
		: QWidget(parent)
		, m_pSerialPortController(nullptr)
		, m_strPortNameIndex("")
	{
		ui.setupUi(this);
		this->initUI();
	}

	SerialPortWidget::~SerialPortWidget()
	{
		if (m_pSerialPortController != nullptr)
		{
			delete m_pSerialPortController;
			m_pSerialPortController = nullptr;
		}
	}

	void SerialPortWidget::slotUpdatePortNameTimeout()
	{
		updateSerialPortList();
	}

	void SerialPortWidget::slotRepeatSendMsgTimeout()
	{
		QString strSendMsg = ui.textEdit_dataSend->toPlainText();
		if (strSendMsg.isEmpty())
		{
			return;
		}

		if (!m_pSerialPortController->getSerialPortIsOpen())
		{
			return;
		}

		m_pSerialPortController->setSendSerialMsg(strSendMsg);

		updateSendMsgList(strSendMsg);
	}

	void SerialPortWidget::slotSeleteSerialPortCurrentTextChanged(const QString & text)
	{
		m_strPortNameIndex = text;
	}

	void SerialPortWidget::slotSendMsgTypeButtonClicked(int id)
	{
		m_pSerialPortController->setSendMsgType(id);
	}

	void SerialPortWidget::slotRecvMsgTypeButtonClicked(int id)
	{
		m_pSerialPortController->setRecvMsgType(id);
	}

	void SerialPortWidget::slotRepeatSendCheckBoxStateChanged(int state)
	{
		if (state == Qt::Checked)
		{
			m_pTimerRepeatSend->start(ui.spinBox_SendRepeatTime->value());
		}
		else if (state == Qt::Unchecked)
		{
			m_pTimerRepeatSend->stop();
		}
		else if (state == Qt::PartiallyChecked)
		{
			qDebug() << "半选中";
		}
	}

	void SerialPortWidget::slotSerialPortControlBtnClicked()
	{
		QPushButton * btn = static_cast<QPushButton*>(sender());
		if (btn == ui.pushButton_open)
		{
			bool state = m_pSerialPortController->setOpenSerialPort(ui.comboBox_portName->currentText()
				, ui.comboBox_baudRate->currentIndex()
				, ui.comboBox_dataBits->currentIndex()
				, ui.comboBox_parity->currentIndex()
				, ui.comboBox_stopBits->currentIndex()
				, ui.comboBox_flowControl->currentIndex());

			MessDispath::instance()->setSystemLogInfo(QString("%1 OPEN ").arg(ui.comboBox_portName->currentText()), state ? 0 : -1);

			if (state)
			{
				ui.pushButton_open->setEnabled(false);
				ui.pushButton_close->setEnabled(true);
			}
		}
		else if(btn == ui.pushButton_close)
		{
			m_pSerialPortController->setCloseSerialPort();
			MessDispath::instance()->setSystemLogInfo(QString("%1 OPEN ").arg(ui.comboBox_portName->currentText()), 0);

			ui.pushButton_open->setEnabled(true);
			ui.pushButton_close->setEnabled(false);
		}
	}

	void SerialPortWidget::slotSerialPortSendMsgBtnClicked()
	{
		QString strSendMsg = ui.textEdit_dataSend->toPlainText();
		if (strSendMsg.isEmpty())
		{
			return;
		}

		if (!m_pSerialPortController->getSerialPortIsOpen())
		{
			return;
		}

		m_pSerialPortController->setSendSerialMsg(strSendMsg);	
		updateSendMsgList(strSendMsg);
	}

	void SerialPortWidget::slotSerialPortMsgInfo(int iMsgType, QString strMsg)
	{
		QString strLog;
		// 是否显示时间
		if (ui.checkBox_showTime->isChecked())
		{
			QDateTime dateTime = QDateTime::currentDateTime();//获取系统当前的时间
			QString strDateTime = dateTime.toString("yyyy-MM-dd hh:mm:ss.zzz");//格式化时间
			strLog = strLog + strDateTime + (iMsgType == 0 ? "[Send]" : "[Recv]") + " : ";
		}

		// 是否显示发送
		if (!ui.checkBox_showSendMsg->isChecked())
		{
			if (iMsgType == 0)
			{
				return;
			}
		}
		ui.plainTextEdit_dataLog->appendPlainText(strLog + strMsg);
	}

	void SerialPortWidget::initUI()
	{
		m_pSerialPortController = new SerialPortController();
		connect(m_pSerialPortController, &SerialPortController::signalSerialPortMsgInfo, this, &SerialPortWidget::slotSerialPortMsgInfo);

		{
			ui.radioButton_sendSetASCII->setChecked(true);

			m_pSendButtonGroup = new QButtonGroup(this);
			m_pSendButtonGroup->addButton(ui.radioButton_sendSetASCII, 0);
			m_pSendButtonGroup->addButton(ui.radioButton_sendSetHex, 1);
			m_pSendButtonGroup->setExclusive(true);
			connect(m_pSendButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
				[this](int id) {
				slotSendMsgTypeButtonClicked(id);
			});
		}

		{
			ui.radioButton_recvSetASCII->setChecked(true);

			m_pRecvButtonGroup = new QButtonGroup(this);
			m_pRecvButtonGroup->addButton(ui.radioButton_recvSetASCII, 0);
			m_pRecvButtonGroup->addButton(ui.radioButton_recvSetHex, 1);
			m_pRecvButtonGroup->setExclusive(true);

			connect(m_pRecvButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
				[this](int id) {
				slotRecvMsgTypeButtonClicked(id);
			});
		}

		{
			m_pTimerUpdate = new QTimer(this);
			//m_pTimerUpdate->start(5000);
			connect(m_pTimerUpdate, &QTimer::timeout, this, &SerialPortWidget::slotUpdatePortNameTimeout);
		}

		{
			m_pTimerRepeatSend = new QTimer(this);
			connect(m_pTimerRepeatSend, &QTimer::timeout, this, &SerialPortWidget::slotRepeatSendMsgTimeout);
		}

		connect(ui.comboBox_portName, &QComboBox::currentTextChanged, this, &SerialPortWidget::slotSeleteSerialPortCurrentTextChanged);

		connect(ui.checkBox_SendRepeat, &QCheckBox::stateChanged, this, &SerialPortWidget::slotRepeatSendCheckBoxStateChanged);

		connect(ui.pushButton_open, &QPushButton::clicked, this, &SerialPortWidget::slotSerialPortControlBtnClicked);
		connect(ui.pushButton_close, &QPushButton::clicked, this, &SerialPortWidget::slotSerialPortControlBtnClicked);
		connect(ui.pushButton_sendMsg, &QPushButton::clicked, this, &SerialPortWidget::slotSerialPortSendMsgBtnClicked);

		updateSerialPortList();

		ui.pushButton_open->setEnabled(true);
		ui.pushButton_close->setEnabled(false);
	}

	void SerialPortWidget::updateSerialPortList()
	{
		ui.comboBox_portName->clear();
		QStringList strList = m_pSerialPortController->getSerialPortList();
		if (strList.isEmpty())
		{
			return;
		}

		for (int i = 0; i < strList.size(); i++)
		{
			QString strPortName = strList.at(i);
			ui.comboBox_portName->addItem(strPortName);
		}

		if (m_strPortNameIndex.isEmpty())
		{
			return;
		}

		ui.comboBox_portName->setCurrentText(m_strPortNameIndex);
	}

	void SerialPortWidget::updateSendMsgList(QString strSnedMsg)
	{
		if(m_strSendMsgList.isEmpty())
		{
			m_strSendMsgList.append(strSnedMsg);
		}
		else
		{
			bool bExist = false;

			foreach (QString strMsg, m_strSendMsgList)
			{
				if (strMsg == strSnedMsg)
				{
					bExist = true;
				}
			} 

			if (!bExist)
			{
				m_strSendMsgList.append(strSnedMsg);
			}
		}

		ui.comboBox_sendLog->clear();
		ui.comboBox_sendLog->addItems(m_strSendMsgList);
		ui.comboBox_sendLog->setCurrentText(strSnedMsg);
	}

}//namespace Core 
